package br.edu.univas.es2012.controller;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import br.edu.univas.es2012.listeners.CadastroServicosListener;
import br.edu.univas.es2012.model.DAOException;
import br.edu.univas.es2012.model.ServicosDAO;
import br.edu.univas.es2012.model.ServicosTO;
import br.edu.univas.es2012.view.FrameConsultaServicos;
import br.edu.univas.es2012.view.FrameServicos;

public class ControllerServicos {
	
	private ServicosDAO servicosDAO;
	private FrameServicos frameServicos;
	private FrameConsultaServicos frameConsultaServicos;
	
	public ControllerServicos(ServicosDAO servicosDAO, FrameConsultaServicos frameConsultaServicos) {
		this.servicosDAO = servicosDAO;
		this.frameConsultaServicos = frameConsultaServicos;
		
	}
	
	public void criarNovoServico(){
		frameServicos = new FrameServicos();
		//frameServicos.getPanelDadosServicos().setData();
		frameServicos.addCadastroServicosListener(new CadastroServicosListener() {
			
			@Override
			public void dadosGravados(ServicosTO servicosTO) {
				inserirServicos(servicosTO);
				frameServicos.dispose();
				try{
					ArrayList<ServicosTO> servicos = servicosDAO.listarTodosServicos();
					frameConsultaServicos.limpaDadosServicos();
					frameConsultaServicos.setCadastroServicosListener(servicos);
				}catch(DAOException e){
					e.printStackTrace();
				}
				
			}
			
			@Override
			public void dadosCancelados() {
				frameServicos.dispose();
				
			}
		});
		frameServicos.setResizable(false);
		frameServicos.setLocationRelativeTo(null);
		frameServicos.setVisible(true);
	}
	
	private void inserirServicos(ServicosTO servicosTO) {
		try{
			servicosDAO.cadastrar(servicosTO);
			
		} catch(DAOException e){
			JOptionPane.showMessageDialog(null, "Ocorreu o seguinte Erro: " + e.getMessage());
			e.printStackTrace();
		}
		
	}

}
